<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>ip管理</el-breadcrumb-item>
      <el-breadcrumb-item>ip列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-form :inline="true" :model="searchIpForm" :rules="searchIpFormRules" ref="searchIpFormRef" size="small">
        <el-form-item label="ip地址" prop="ip">
          <el-input v-model="searchIpForm.ip" @keyup.enter.native="searchIp" placeholder="192.168.6.1 or 6.1"></el-input>
        </el-form-item>
        <el-form-item label="mac地址" prop="mac">
          <el-input v-model="searchIpForm.mac" @keyup.enter.native="searchIp" placeholder="xxxx-xxxx-xxxx"></el-input>
        </el-form-item>
        <el-form-item label="使用人" prop="user">
          <el-input v-model="searchIpForm.user" @keyup.enter.native="searchIp" placeholder="张三"></el-input>
        </el-form-item>
        <el-form-item label="网段" prop="segment">
          <el-select v-model="searchIpForm.segment">
            <el-option v-for="seg in segmentListData" :label="seg.segment" :key="seg.id" :value="seg.segment"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchIp">查询</el-button>
          <el-button type="warning" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-table :data="ipListData" border stripe style="width: 100%" size="mini">
        <el-table-column prop="ip" label="ip地址" width="115" align="center"></el-table-column>
        <el-table-column prop="mask" label="掩码" width="115" align="center"></el-table-column>
        <el-table-column prop="gateway" label="网关" width="115" align="center"></el-table-column>
        <el-table-column prop="dns" label="dns" width="115" align="center"> 114.114.114.114 </el-table-column>
        <el-table-column prop="mac" label="物理地址" width="115" align="center"></el-table-column>
        <el-table-column prop="user" label="用户" width="115" align="center"></el-table-column>
        <el-table-column prop="remarks" label="备注" width="120" align="center"></el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.create_time ? scope.row.create_time.substr(0, 19).replace('T', ' ') : '' }} </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button class="tableRowButton" @click="editIp(scope.row)" type="primary" size="mini">授权</el-button>
            <el-popover placement="top" width="80" :ref="`popover-${scope.$index}`">
              <p>确定要解绑ip吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="cancelAction(scope)">取消</el-button>
                <el-button type="primary" @click="undoIp(scope)" size="mini">确定</el-button>
              </div>
              <el-button slot="reference" type="danger" size="mini">解绑</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchIpForm.pagenum"
        :page-sizes="[10, 20, 50]"
        :page-size="searchIpForm.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="编辑信息" :visible.sync="dialogIpEditFormVisible">
      <el-form :model="ipEditForm" size="small" label-width="50px" label-position="right">
        <el-form-item label="ip">
          <el-input v-model="ipEditForm.ip" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="mac">
          <el-input v-model="ipEditForm.mac" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户">
          <el-input v-model="ipEditForm.user" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="设备">
          <el-input v-model="ipEditForm.equipment" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="ipEditForm.remarks" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editSaveIp" size="small">保 存</el-button>
        <el-button @click="dialogIpEditFormVisible = false" size="small">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchIpForm: {
        ip: '',
        mac: '',
        user: '',
        segment: '',
        pagenum: 1,
        pagesize: 10
      },
      searchIpFormRules: {
        ip: [{ max: 15, message: '长度不大于15个字符', trigger: 'blur' }],
        mac: [{ max: 17, message: '长度不大于17个字符', trigger: 'blur' }],
        user: [{ max: 8, message: '长度不大于8个字符', trigger: 'blur' }],
        segment: []
      },
      ipListData: [],
      total: 0,
      ipEditForm: {},
      dialogIpEditFormVisible: false,
      segmentListData: []
    }
  },
  created() {
    this.getIpList()
    this.getSegment()
  },
  methods: {
    // 搜索
    searchIp() {
      this.$refs.searchIpFormRef.validate((valid) => {
        if (valid === false) return false
        this.getIpList()
      })
    },
    // 重置
    resetForm() {
      this.$refs.searchIpFormRef.resetFields()
      this.getIpList()
    },
    // 获取ip方法
    async getIpList() {
      const { data: res } = await this.$http.post('api/ip/list', this.searchIpForm)
      this.ipListData = res.data
      this.total = res.total
    },
    // 调整每页显示行数事件
    handleSizeChange(size) {
      this.searchIpForm.pagesize = size
      this.getIpList()
    },
    // 翻页事件
    handleCurrentChange(num) {
      this.searchIpForm.pagenum = num
      this.getIpList()
    },
    // 授权
    editIp(row) {
      this.dialogIpEditFormVisible = true
      this.ipEditForm = JSON.parse(JSON.stringify(row))
    },
    async editSaveIp() {
      const myDate = new Date()
      const y = myDate.getFullYear()
      const M = myDate.getMonth() + 1
      const d = myDate.getDate()
      const h = myDate.getHours()
      const m = myDate.getMinutes()
      const s = myDate.getSeconds()
      const myDateTime = `${y}-${M}-${d} ${h}:${m}:${s}`
      this.$set(this.ipEditForm, 'create_time', myDateTime)
      console.log(this.ipEditForm)
      const { data: res } = await this.$http.put(`/api/ip/${this.ipEditForm.id}`, this.ipEditForm)
      if (res.code !== 0) return this.$message.error('保存失败！')
      this.$message.success('保存成功！')
      this.dialogIpEditFormVisible = false
      this.getIpList()
    },
    // 删除
    async undoIp(scope) {
      console.log(this.ipEditForm)
      const { data: res } = await this.$http.put(`/api/ip/${scope.row.id}`, { mac: '', user: '', remarks: '', create_time: '' })
      if (res.code !== 0) return this.$message.error('解绑失败！')
      this.$message.success('解绑成功！')
      scope._self.$refs[`popover-${scope.$index}`].doClose()
      this.getIpList()
    },
    // 取消操作
    cancelAction(scope) {
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    // 获取网段
    async getSegment() {
      const { data: res } = await this.$http.get('api/ip/segment')
      this.segmentListData = res.data
      console.log(res.data)
    }
  }
}
</script>

<style lang="less" scoped>
.tableRowButton {
  margin-right: 5px;
}
</style>
